const proxyModal = require('koa2-proxy-middleware')
let { open, hosts } = global.config.proxy

module.exports = function (app) {
  if (open) {
    let targets = hosts.reduce((res, h) => {
      h.prefix.forEach(p => {
        res[p + '(.*)'] = {
          target: h.host,
          changeOrigin: true,
        }
      })
      return res
    }, {})
    app.use(proxyModal({ targets }))
  }
}